<?php
	if (isset($_SESSION[''.SECURITY_SESSION.'']['user_id'])) {
		$start_date = isset($_POST['start_date']) ? $_POST['start_date'] : NULL;
		$shour = isset($_POST['shour']) ? $_POST['shour'] : NULL;
		$smin = isset($_POST['smin']) ? $_POST['smin'] : NULL;
		$duration = isset($_POST['duration']) ? $_POST['duration'] : NULL;
		$m = isset($_REQUEST['m']) ? $_REQUEST['m'] : date("m", time());
		$y = isset($_REQUEST['y']) ? $_REQUEST['y'] : date("Y", time());
		$now_date = date("Y-m-d H:i:s", time());
		$user_id = $_SESSION[''.SECURITY_SESSION.'']['user_id'];
		$invalid_time = TRUE;

		if (isset($start_date)) {
			$timestamp = strtotime("".$start_date." ".$shour.":".$smin.":00");
			
			//Data validation to advise user to select a date after now
			if (!($timestamp >= time() && $start_date != NULL)) {
				set_message('Select a present or future date!','alert',1);
			} else {
				$invalid_time = FALSE;
			}
		}

		$start_time = $start_date . " " . $shour . ":" . $smin . ":" . "00";
		$asked_date = strtotime($start_time);
		
		$book_resources = isset($_REQUEST['book_resources']) ? $_REQUEST['book_resources'] : NULL;

		if((isset($_REQUEST['reserve']) && $_REQUEST['reserve'] == "true") && isset($_REQUEST['start']) && count($book_resources) > 0){
			$book_begin_time = $_REQUEST['start'];
			$book_end_time = $_REQUEST['end'];
			
			// Creating a new Slice:
			$slice_data = array(
							'slice_data' => array(
												'name' => 'WEB_SLICE'
											),
							'expire_date' => $book_end_time
						  );
							
			$slice_id = api_call("add_slice", $slice_data);
			$add_user_to_slice = api_call("add_user_to_slice", array('user_id' => $user_id, 'slice_id' => $slice_id['method_result']));
			
			for($i = 0; $i < count($book_resources); $i++){
				$data = array(
							'resource_id' => $book_resources[$i],
							'slice_id' => $slice_id['method_result']
						);
						
				api_call("add_resource_to_slice", $data);
			}
			
			$reservation_data = array(
									'user_id' => $user_id,
									'slice_id' => $slice_id['method_result'],
									'begin_time' => $book_begin_time,
									'end_time' => $book_end_time
								);
								
			$create_reservation = api_call("create_reservation", $reservation_data);
			
			if($create_reservation['method_result'] == true)
				set_message('Your Nodes and your Frequencies have been booked! Check My Reservations!','success',1);
			else
				set_message('Error in Reservation, try again','error',1);
		}
	}else
		set_message('You do not have a slice! Please contact LABORA for a slice creation!','error',1);
?>


<body>
	<div id='text'>
		<div class='title'>
			<?php echo bread_crumbs(NULL, TRUE); ?> Reservation - <?php echo $_SESSION[''.SECURITY_SESSION.'']['username']; ?>
		</div>
	</div>
	<table id='reservation_table'> 
		<tr>
			<td class='centralize' colspan='2'>
				 Current Server Time: <?php echo $now_date; ?>
			</td>
		</tr>
		<tr>
			<td>
				<?php mk_drawCalendar($m, $y); ?>
			</td>
			<td>
				<form name="f" action="?page=reservation&m=<?php echo $m; ?>&y=<?php echo $y; ?>" method="POST">
					<table height="153px">
						<tr>
							<td colspan="2" bgcolor="#000000">
								<font color="#FFFFFF"><b>Select Start Time and Duration</b></font>
							</td>
						</tr>
						<tr>
							<td>
								<b>Start Date: </b>
							</td>
							<td>
								<input type="text" name="start_date" size="10" value="<?php echo $start_date; ?>" readonly> <font size="2">yyyy-mm-dd</font>
							</td>
						</tr>	
						<tr>
							<td>
								<b>Start Time: </b>
							</td>
							<td>
								<select name="shour">
									<?php
									  for($i=0;$i<24;$i++){

										$s_hour=($i<10) ? $i="0" . $i : $i;
										
										if($s_hour == $shour)
											echo '<option value="' . $s_hour . '" selected>' . $s_hour . '</option>';
										else
											echo '<option value="' . $s_hour . '">' . $s_hour . '</option>';
									  }
									?>
								</select>
								<font size="2">hh</font>
								
								<select name="smin">
									<?php
										if($smin == '30')
											echo '<option value="00">00</option>
											<option value="30" selected>30</option>';
										else
											echo '<option value="00" selected>00</option>
											<option value="30">30</option>';
									?>
								</select>
								<font size="2"> min</font>
							</td>
						</tr>
						<tr>
							<td>
								<b>Duration: </b>
							</td>
							<td>
								<select name="duration">
									<?php
									  for($i=0.5;$i<4.5;$i=$i+0.5){
										if($duration == $i)
											echo '<option value="' . $i . '" selected>' . $i . '</option>';
										else
											echo '<option value="' . $i . '">' . $i . '</option>';
									  }
									?>
								</select>
								<font size="2"> hours</font>
							</td>
						</tr>
						<tr>
							<td colspan="2" align="center" bgcolor="#CCCCCC">
								<input type="submit" value="Check Available Nodes" />
							</td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
	
	<?php
		if (!$invalid_time) {
			$tok2 = strtok($duration, ".");
			$th=$tok2;
			
			while ($tok2 !== false){    
				$tok2 = strtok(".");
				if($tok2 !== false) { $tm = $tok2; }
			}

			$end_time = date("Y-m-d H:i:s", mktime($shour+$th, $smin+($tm*6), 0, date('m', $asked_date), date('d', $asked_date), date('Y', $asked_date)));

			echo "<div class='available_nc'>Available Nodes and Channels between <b>" . $start_time . "</b> and <b>" . $end_time . "</b> :</div>";
			
			echo "<table align='center'>
					<tr>
						<td>
							<form action='' method='post'>
							<input type='hidden' name='start' value='" . $start_time . "'>
							<input type='hidden' name='end' value='" . $end_time . "'>
							<input type='hidden' name='reserve' value='true'>
							<input type='submit' value='Reserve' style='background-color:#3C639F;color:white' />
						</td>
					</tr>
					</table>";
					
			echo "<div>
					<table id='reserve' cellpadding='10' align='center'>
						<tr class='top'>
							<td>
								<span>Orbit/Diskless<br/>Nodes</span><br/>
								<input type='checkbox' onclick='checkCheckbox(\"node_list\", this)' />
							</td>
							<td>
								<span>802.11a</span><br/>
								<input type='checkbox' onclick='checkCheckbox(\"channel_a_list\", this)' />
							</td>
							<td>
								<span>802.11b/g</span><br/>
								<input type='checkbox' onclick='checkCheckbox(\"channel_g_list\", this)' />
							</td>
						</tr>
						<tr>";
						
			$get_node_data = array(
								'begin_time' => $start_time,
								'end_time' => $end_time,
								'type' => 'node'
							 );
			$nodes = api_call("get_free_resources", $get_node_data);
			
			$get_channel_data = array(
									'begin_time' => $start_time,
									'end_time' => $end_time,
									'type' => 'channel'
								);
			$channels = api_call("get_free_resources", $get_channel_data);

			if(count($nodes['method_result']))
				$nodes = api_call("get_resources", array('filter' => $nodes['method_result']));
			
			$channels_a = array();
			$channels_g = array();
			if(count($channels['method_result'])){
				$channels = api_call("get_resources", array('filter' => $channels['method_result']));
				
				for ($i = 0; $i < count($channels['method_result']); $i++){
					if($channels['method_result'][$i]['resource_details']['modulation'] == 'IEEE802_11a')
						$channels_a[] = $channels['method_result'][$i];
					else if($channels['method_result'][$i]['resource_details']['modulation'] == 'IEEE802_11b_g')
						$channels_g[] = $channels['method_result'][$i];
				}
			}
			
			echo "<td align='right' id='node_list'>";
					
			for ($i=0; $i < count($nodes['method_result']); $i++){
				
				$node_description = "<center><b>Node description:</b></center>" .
									"<b>Hostname:</b> ".$nodes['method_result'][$i]['resource_details']['hostname']."<br>" .
									"<b>Control IP:</b> ".$nodes['method_result'][$i]['resource_details']['control_ip']."<br>" .
									"<b>HRN:</b> ".$nodes['method_result'][$i]['resource_details']['hrn']."";
				
				echo "<div onMouseOver=\"toolTip('".$node_description."', 200)\" onMouseOut=\"toolTip()\">";
				echo "" . $nodes['method_result'][$i]['resource_details']['hostname'] . ": ";
				echo "<input type='checkbox' name='book_resources[]' value='" . $nodes['method_result'][$i]['id'] . "' />";
				echo "</div>";
			}
				
			echo "</td>";
			echo "<td align='right' id='channel_a_list'>";
			
			for ($k = 0; $k < count($channels_a); $k++){
				echo "<div>";
				echo "Channel " . $channels_a[$k]['resource_details']['channel_number'] . ": ";
				echo "<input type='checkbox' name='book_resources[]' value='" . $channels_a[$k]['id'] . "' />";
				echo "</div>";
			}
			
			echo "</td>";
			echo "<td align='right' id='channel_g_list'>";
			
			for ($k = 0; $k < count($channels_g); $k++){
				echo "<div>";
				echo "Channel " . $channels_g[$k]['resource_details']['channel_number'] . ": ";
				echo "<input type='checkbox' name='book_resources[]' value='" . $channels_g[$k]['id'] . "' />";
				echo "</div>";
			}
			
			echo "</td></tr>";
			echo "</table></div>";

			echo "</form>";
		}
	?>
</body>
